package karileet.meeting.profile.list.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import karileet.meeting.profile.R;
import karileet.meeting.profile.list.holder.ListCheckboxItemHolder;
import karileet.meeting.profile.list.item.ListCheckboxItem;

import java.util.List;

public class ListCheckboxItemAdapter extends ArrayAdapter<ListCheckboxItem> {

    private final List<ListCheckboxItem> list;
    private final Activity context;

    public ListCheckboxItemAdapter(Activity context, List<ListCheckboxItem> list) {
        super(context, R.layout.list_checkbox_item, list);
        this.context = context;
        this.list = list;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflator = context.getLayoutInflater();
        View view = inflator.inflate(R.layout.list_checkbox_item, null);
        final ListCheckboxItemHolder listCheckboxItemHolder = new ListCheckboxItemHolder();

        listCheckboxItemHolder.setTitle((TextView) view.findViewById(R.id.rowTitle));
        CheckBox cb = (CheckBox) view.findViewById(R.id.rowActive);
        cb.setChecked(list.get(position).isSelected());
        listCheckboxItemHolder.setCb(cb);
        view.setTag(listCheckboxItemHolder);
        listCheckboxItemHolder.setTag(list.get(position));

        if (convertView != null) {
            view = convertView;
            //something i do not understand is omitted
        }


        ListCheckboxItemHolder holder = (ListCheckboxItemHolder) view.getTag();
        holder.setTitleText(list.get(position).getTitle());
        holder.check(list.get(position).isSelected());
        final int pos = position;

        // Prevent users from clicking checkbox (no action will be fired when clicking it)
        holder.getCb().setClickable(false);
        return view;
    }
}
